﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace UI
{
    public delegate void ChangeTextHandler();
    public partial class FManageAccount : Form
    {
        private AccountCollection accList = new AccountCollection();
        public event EventHandler refreshData = null;
        public FManageAccount()
        {
            InitializeComponent();
           
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            FCreateAccount fca = new FCreateAccount();
            fca.ShowDialog();
            accList = FMain.accMan.AccountList;
            LoadDataToGrid(accList);    
        }

        private void FManageAccount_Load(object sender, EventArgs e)
        {
            accList = FMain.accMan.AccountList;
            LoadDataToGrid(accList);
        }

        private void LoadDataToGrid(AccountCollection accList)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID"));
            dt.Columns.Add(new DataColumn("Name"));
            dt.Columns.Add(new DataColumn("Color"));
            dt.Columns.Add(new DataColumn("CreatedTime"));
            dt.Columns.Add(new DataColumn("ModifiedTime"));
            dt.Columns.Add(new DataColumn("Balance"));
            for (int i = 0; i < accList.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = accList[i].ID;
                dr[1] = accList[i].Name;
                dr[2] = accList[i].Color;
                dr[3] = accList[i].CreatedTime.ToString("yyyy-MM-dd");
                dr[4] = accList[i].ModifiedTime.ToString("yyyy-MM-dd");
                dr[5] = accList[i].Balance.ToString();
                dt.Rows.Add(dr);

            }
            dgv_Account.DataSource = dt;

        }

        private void dgv_Account_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                e.CellStyle.BackColor = Color.FromArgb(Convert.ToInt32(e.Value));
                dgv_Account.Rows[e.RowIndex].DefaultCellStyle.ForeColor = e.CellStyle.BackColor;
                dgv_Account.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = e.CellStyle.BackColor;
                e.Value = "";
            }
          
        }

        protected void OnRefreshData()
        {
            if (refreshData != null)
            {
                refreshData(this, EventArgs.Empty);
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int index = dgv_Account.CurrentRow.Index;
            int iAccountID =Int32.Parse(dgv_Account.Rows[index].Cells[0].Value.ToString());
            FCreateAccount fca = new FCreateAccount(iAccountID);
            fca.ShowDialog();
            accList = FMain.accMan.AccountList;
            LoadDataToGrid(accList);  

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            int index = dgv_Account.CurrentRow.Index;
            int iAccountID = Int32.Parse(dgv_Account.Rows[index].Cells[0].Value.ToString());
            FMain.accMan.DelAccount(iAccountID);
            accList = FMain.accMan.AccountList;
            LoadDataToGrid(accList);  
        }


    }
}
